package com.pw.study.flink.transform;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class TransFormMap {
    public static void main(String[] args) {
        //dealMap();
        //dealMap2();
        //dealMap3();
        dealMap4();

    }

    private static void dealMap4() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        env.fromElements(1, 2, 3, 4, 5, 6, 7).map(new MyRichMapFunction()).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealMap3() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.fromElements(1, 2, 3, 4, 5, 6).map(new MyMapFunction()).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealMap2() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.fromElements(1, 2, 3, 4, 5, 6).map(node -> node * node).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealMap() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4, 5, 6);
        stream.map(new MapFunction<Integer, Integer>() {
            @Override
            public Integer map(Integer value) throws Exception {
                return value * value;
            }
        }).print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MyMapFunction implements MapFunction<Integer, Integer> {

        @Override
        public Integer map(Integer v) throws Exception {
            return v * v;
        }
    }

    public static class MyRichMapFunction extends RichMapFunction<Integer, Integer> {

        @Override
        public void open(Configuration parameters) throws Exception {
            System.out.println("open....................");
            super.open(parameters);
        }

        @Override
        public void close() throws Exception {
            System.out.println("close........................");
            super.close();
        }

        @Override
        public Integer map(Integer v) throws Exception {
            System.out.println("map...............");
            return v * v;
        }
    }
}
